#!/bin/sh

WORKER_PAYLOAD=/etc/concourse-beacon/worker.json
WORKER_KEY=/etc/concourse-beacon/keypair/id_rsa

if [ "$#" != 1 ]; then
  echo "usage: $0 ip:port" >&2
  exit 1
fi

if [ ! -f "$WORKER_PAYLOAD" ]; then
  echo "must create $WORKER_PAYLOAD" >&2
  exit 1
fi

if [ ! -f "$WORKER_KEY" ]; then
  echo "must create $WORKER_KEY" >&2
  exit 1
fi

HOST="$(echo $1 | cut -d: -f1)"
PORT="$(echo $1 | cut -d: -f2)"

if [ -z "$HOST" ] || [ -z "$PORT" ]; then
  echo "must specify host:port" >&2
  exit 1
fi

exec ssh \
  -o UserKnownHostsFile=/etc/concourse-beacon/known_hosts \
  -o ConnectTimeout=30 \
  -o ServerAliveInterval=8 \
  -o ServerAliveCountMax=3 \
  -i "$WORKER_KEY" \
  -R 0.0.0.0:7777:127.0.0.1:7777 \
  -R 0.0.0.0:7788:127.0.0.1:7788 \
  -p "$PORT" \
  "$HOST" \
  forward-worker \
    --garden 0.0.0.0:7777 \
    --baggageclaim 0.0.0.0:7788 \
    < "$WORKER_PAYLOAD"
